package net.zigin.nhi.could.waste.service.impl;

import com.alibaba.fastjson.JSONObject;
import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.dto.SysUserDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.service.SysMenuService;
import net.zigin.nhi.could.waste.service.SysRoleService;
import net.zigin.nhi.could.waste.util.HttpClientUtil;
import net.zigin.nhi.could.waste.util.JWTUtil;
import net.zigin.nhi.could.waste.util.SecurityUtil;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import net.zigin.nhi.could.waste.vo.SysMenuVo;
import net.zigin.nhi.could.waste.vo.SysRoleVo;
import net.zigin.nhi.could.waste.vo.SysUserVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysUserMapper;
import net.zigin.nhi.could.waste.model.SysUser;
import net.zigin.nhi.could.waste.service.SysUserService;

import java.util.List;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-7 19:08:56                                               
##################################################################*/


/**
 * SysUser服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUserServiceImpl
 * @date 2021-9-7 19:08:56
 */
@Service
public class SysUserServiceImpl extends AbstractServiceImpl<SysUser, String> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysRoleService sysRoleService;
    @Value("${zigin.smsUrl}")
    private String smsUrl;

    @Override
    public BaseMapper<SysUser, String> getMapper() {
        return sysUserMapper;
    }

    @Override
    public SysUserVo getUserByUserAccount(String userAccount) {
        SysUser user = sysUserMapper.getUserByLoginName(userAccount);
        return ReflectUtil.copyProperties(user, SysUserVo.class);
    }

    @Override
    public SysUserVo getInfoById(String id) {
        SysUser user = sysUserMapper.selectByPrimaryKey(id);
        SysUserVo userVo = ReflectUtil.copyProperties(user, SysUserVo.class);
        List<SysRoleVo> roleVos = sysRoleService.infoByUserId(id);
        userVo.setRoleVoList(roleVos);
        List<SysMenuVo> menuVos = sysMenuService.infoByUserId(id);
        AssertUtil.isNull(menuVos,"该账号下没有app端菜单！");
        userVo.setMenuVoList(menuVos);
        return userVo;
    }

    @Override
    public void modifyPassword(SysUserDto userDto) {
        AssertUtil.isNull(userDto, "用户信息不能为空");
        AssertUtil.isNull(userDto.getName(), "用户账户不能为空");
        AssertUtil.isNull(userDto.getPassword(), "用户新密码不能为空");
        AssertUtil.isNull(userDto.getOldPassword(), "用户旧密码不能为空");
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        // 允许修改其他人密码
       /* UserVo currentUser = getCurrentUser();
        if(!userDto.getUserAccount().equals(currentUser.getUserAccount())){
            throw new EpiCloudException("只能修改自己的账户密码！");
        }*/
        if (userDto.getPassword().equals(userDto.getOldPassword())) {
            throw new EpiCloudException("新密码不能与旧密码相同！");
        }
        // 通过账户获取用户信息
        SysUserVo userVo = getUserByUserAccount(userDto.getName());
        if (userVo == null) {
            throw new EpiCloudException("账户不存在！");
        }
        // 旧密码与新密码校验
        if (!SecurityUtil.matchesPassword(userDto.getOldPassword(),userVo.getPassword())) {
            throw new EpiCloudException("旧密码错误！");
        }
        // 修改密码
        userVo.setPassword(SecurityUtil.encodePassword(userDto.getPassword()));
        userVo.setModifyUser(tokenInfo.getLoginName());
        SysUser user = ReflectUtil.copyProperties(userVo, SysUser.class);
        sysUserMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public SysUserVo getUserByUserPhone(String mobile) {
        SysUser user = sysUserMapper.getUserByUserPhone(mobile);
        AssertUtil.isNull(user, "用户不存在！");
        SysUserVo userVo = ReflectUtil.copyProperties(user, SysUserVo.class);
        List<SysRoleVo> roleVos = sysRoleService.infoByUserId(user.getId());
        userVo.setRoleVoList(roleVos);
        List<SysMenuVo> menuVos = sysMenuService.infoByUserId(user.getId());
        userVo.setMenuVoList(menuVos);
        return userVo;
    }

    @Override
    public String sendMessage(String mobile) {
        String resultStr = HttpClientUtil.smsCode(smsUrl, mobile);
        JsonForResult jsonForResult = JSONObject.parseObject(resultStr, JsonForResult.class);
        return jsonForResult.getData().toString();
    }

    @Override
    public SysUserVo getCurrentUser() {
        String userAccount = JWTUtil.getUserAccount(SecurityUtils.getSubject().getPrincipal().toString());
        return sysUserMapper.getUserInfoByLoginName(userAccount);
    }
}